 package com.ruoyi.webs.utils;

 import java.security.Key;
 import java.security.SecureRandom;
 import javax.crypto.Cipher;
 import javax.crypto.KeyGenerator;
 import org.apache.commons.codec.DecoderException;
 import org.apache.commons.codec.binary.Base32;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;


 public class CryptoUtil
 {
   public static Key DEFAULT_KEY = null;

   public static final String DEFAULT_SECRET_KEY1 = "?:P)(OL><KI*&UJMNHY^%TGBVFR$#EDCXSW@!QAZ";

   public static final String DEFAULT_SECRET_KEY2 = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/";

   public static final String DEFAULT_SECRET_KEY3 = "!QAZ@WSX#EDC$RFV%TGB^YHN&UJM*IK<(OL>)P:?";
   public static final String DEFAULT_SECRET_KEY4 = "1qaz@WSX3edc$RFV5tgb^YHN7ujm*IK<9ol.)P:?";
   public static final String DEFAULT_SECRET_KEY5 = "!QAZ2wsx#EDC4rfv%TGB6yhn&UJM8ik,(OL>0p;/";
   public static final String DEFAULT_SECRET_KEY6 = "1qaz2wsx3edc4rfv5tgb^YHN&UJM*IK<(OL>)P:?";
   public static final String DEFAULT_SECRET_KEY7 = "1hlk2sdf3ewg4hgh5tku^HRH&NTR*NG<(BT>)H:`";
   public static final String DEFAULT_SECRET_KEY8 = "9hlk3sdf5ewg2hgh6tku^MDF&HTU*NG<(DE>)H:`";
   public static final String DEFAULT_SECRET_KEY = "?:P)(OL><KI*&UJMNHY^%TGBVFR$#EDCXSW@!QAZ";
   public static final String DES = "DES";
   public static final Base32 base32 = new Base32();

   static {
     DEFAULT_KEY = obtainKey("?:P)(OL><KI*&UJMNHY^%TGBVFR$#EDCXSW@!QAZ");
   }




   public static Key obtainKey(String key) {
     if (key == null) {
       return DEFAULT_KEY;
     }
     KeyGenerator generator = null;
     try {
       generator = KeyGenerator.getInstance("DES");

       SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
       secureRandom.setSeed(key.getBytes("UTF-8"));
       generator.init(secureRandom);
     } catch (Exception e) {
       e.printStackTrace();
     }
     Key key1 = generator.generateKey();
     generator = null;
     return key1;
   }





   public static String encode(String str) {
     return encode64(null, str);
   }





   public static String encode64(String key, String str) {
     return Base64.encodeBase64URLSafeString(obtainEncode(key, str.getBytes()));
   }





   public static String encode32(String key, String str) {
     return base32.encodeAsString(obtainEncode(key, str.getBytes())).replaceAll("=", "");
   }





   public static String encode16(String key, String str) {
     return Hex.encodeHexString(obtainEncode(key, str.getBytes()));
   }





   public static String decode(String str) {
     return decode64(null, str);
   }





   public static String decode64(String key, String str) {
    return new String(obtainDecode(key, Base64.decodeBase64(str)));
   }





   public static String decode32(String key, String str) {
    return new String(obtainDecode(key, base32.decode(str)));
   }





   public static String decode16(String key, String str) {
     try {
      return new String(obtainDecode(key, Hex.decodeHex(str.toCharArray())));
    } catch (DecoderException e) {
      e.printStackTrace();

      return null;
     }
   }



   private static byte[] obtainEncode(String key, byte[] str) {
     Cipher cipher;
    byte[] byteFina = null;

     try {
      Key key1 = obtainKey(key);
      Cipher cipher1 = Cipher.getInstance("DES");
      cipher1.init(1, key1);
      byteFina = cipher1.doFinal(str);
    } catch (Exception e) {
      e.printStackTrace();
     } finally {
      Cipher cipher1 = null;
     }
    return byteFina;
   }





   private static byte[] obtainDecode(String key, byte[] str) {
     Cipher cipher;
    byte[] byteFina = null;
     try {
      Key key1 = obtainKey(key);
      Cipher cipher1 = Cipher.getInstance("DES");
      cipher1.init(2, key1);
      byteFina = cipher1.doFinal(str);
    } catch (Exception e) {
      e.printStackTrace();
     } finally {
      Cipher cipher1 = null;
     }
    return byteFina;
   }






   public static String encrypt(String val) {
    String m = encode64("1hlk2sdf3ewg4hgh5tku^HRH&NTR*NG<(BT>)H:`", val);
    String m1 = encode32("9hlk3sdf5ewg2hgh6tku^MDF&HTU*NG<(DE>)H:`", m);
    return m1;
   }







   public static String decrypt(String val) {
    String n = decode32("9hlk3sdf5ewg2hgh6tku^MDF&HTU*NG<(DE>)H:`", val);
    String n1 = decode64("1hlk2sdf3ewg4hgh5tku^HRH&NTR*NG<(BT>)H:`", n);
    return n1;
   }


   public static void main(String[] args) {
    String m = encrypt("123456");
    System.out.println("加密结果：" + m);

    String n = decrypt(m);
    System.out.println("解密结果：" + n);
   }
 }
